﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Microsoft.SharePoint.Administration;
using ServBus.ListForm2010.Setup.Utilities;

namespace ServBus.ListForm2010.Setup.Validators
{
    public class SPAdminValidator : BaseValidator
    {
        public override string CheckItemText
        {
            get { return Properties.Resources.CheckingSPAdmin; }
        }

        public override string ValidText
        {
            get { return Properties.Resources.CheckPassSPAdmin; }
        }

        public override string InvalidText
        {
            get { return Properties.Resources.CheckFailSPAdmin; }
        }

        public override void Validate()
        {
            this.Validated = false;
            Log.WriteLog(Log.LogType.Info, "Start Adm Service Check...");
            Log.WriteLog(Log.LogType.Info, "Server Mount:"+ SPFarm.Local.Servers.Count);
            try
            {
                foreach (SPServer server in SPFarm.Local.Servers)
                {
                    Log.WriteLog(Log.LogType.Info, "Server Name:" + server.Name);
                    foreach (SPServiceInstance svc in server.ServiceInstances)
                    {
                        if (svc.GetType().FullName == "Microsoft.SharePoint.Administration.SPAdministrationServiceInstance")
                        {
                            ServiceController sc = new ServiceController("SPAdminV4", server.Name);
                            Log.WriteLog(Log.LogType.Info, "sc Status:" + sc.Status);
                            if (sc.Status == ServiceControllerStatus.Running)
                            {
                                this.Validated = true;
                                return;
                            }
                            break;
                        }
                    }
                }
            }
            catch(Exception mm)
            {
                Log.WriteLog(Log.LogType.Info, "Adm Service Error:" + mm.ToString());
 
            }
        }
    }
}
